# Author: Andrea Junqueira
# Project: Janusz, Cunha, and Junqueira
# Goal: Run RD models for Women (Main Analysis)
# First Version: June 22nd, 2023
# This Version: June 11th, 2024

rm(list = ls())

library(here)
library(rdrobust)

# Read genFunctions
source(here("R-Files", "genFunctions.R"))

# Load data
load(file = here::here("Data", "womenRDD_final.RData"))

# Indicator for robustness checks. Change it for TRUE if you want to run robustness checks
ROBUST <- FALSE

# Session ran on 06/11/2024
# > sessionInfo()
# R version 4.1.2 (2021-11-01)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Linux Mint 21.1
# 
# Matrix products: default
# BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
# LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
# 
# locale:
#   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
# [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
# [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] rdrobust_2.1.1 here_1.0.1    
# 
# loaded via a namespace (and not attached):
# [1] rstudioapi_0.14  magrittr_2.0.3   MASS_7.3-55      tidyselect_1.2.0 munsell_0.5.0    colorspace_2.1-0 R6_2.5.1        
# [8] rlang_1.1.1      fansi_1.0.4      dplyr_1.1.2      tools_4.1.2      grid_4.1.2       gtable_0.3.3     utf8_1.2.3      
# [15] cli_3.6.1        withr_2.5.0      rprojroot_2.0.3  tibble_3.2.1     lifecycle_1.0.3  farver_2.1.1     ggplot2_3.4.2   
# [22] vctrs_0.6.3      glue_1.6.2       labeling_0.4.2   stringi_1.7.12   compiler_4.1.2   pillar_1.9.0     generics_0.1.3  
# [29] scales_1.2.1     pkgconfig_2.0.3 


################################################
########### Part 1 - No Restrictions ###########
################################################

# Women Candidates
m1a <- rdrobust(y = womenRDD$totalWomen,
                x = womenRDD$run_var_woman,
                all = TRUE)
summary(m1a) # Negative, p <.05

# Women Candidates (Ratio)
m1b <- rdrobust(y = womenRDD$totalWomen_ratio,
                x = womenRDD$run_var_woman,
                all = TRUE)
summary(m1b) # Negative, Null

# Women Candidates (pct)
m1c <- rdrobust(y = womenRDD$totalWomen_pct,
                x = womenRDD$run_var_woman,
                all = TRUE)
summary(m1c) # Positive, Null


# Figures
pdf(here('plots_tables', 'WomenCandidates.pdf'), height = 8, width = 8)
genPlot(mydata = womenRDD, DV = 'totalWomen', dvName = '# of Women Candidates', run_var = 'run_var_woman', ylim = c(10, 60), t1 = 60, t2 = 55)
genPlot(mydata = womenRDD, DV = 'totalWomen_ratio', dvName = 'Ratio Women Candidates Per Seat', run_var = 'run_var_woman', ylim = c(1, 4), t1 = 4, t2 = 3.7)
genPlot(mydata = womenRDD, DV = 'totalWomen_pct', dvName = '% of Women Candidates', run_var = 'run_var_woman', ylim = c(33, 35), t1 = 35, t2 = 34.75)
dev.off()

#############################################
########### Part 2 - No Experience ##########
#############################################

# Women Candidates
m2a  <- rdrobust(y = womenRDD$totalWomen_noexp,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(m2a) # Negative, p <.05

# Women Candidates (ratio)
m2b  <- rdrobust(y = womenRDD$totalWomen_noexp_ratio,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(m2b) # Negative, Null

# Women Candidates (pct)
m2c  <- rdrobust(y = womenRDD$totalWomen_noexp_pct,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(m2c) # Negative, Null

# Figures
pdf(here::here('plots_tables', 'WomenCandidates_NoExp.pdf'), height = 8, width = 8)
genPlot(mydata = womenRDD, DV = 'totalWomen_noexp', dvName = '# of Women Candidates', run_var = 'run_var_woman', ylim = c(5, 45), t1 = 45, t2 = 41)
genPlot(mydata = womenRDD, DV = 'totalWomen_noexp_ratio', dvName = 'Ratio Women Candidates Per Seat', run_var = 'run_var_woman', ylim = c(1, 3), t1 = 2.9, t2 = 2.7)
genPlot(mydata = womenRDD, DV = 'totalWomen_noexp_pct', dvName = '% of Women Candidates', run_var = 'run_var_woman', ylim = c(38, 46), t1 = 46, t2 = 45)
dev.off()

##########################################################
########### Part 3 - No Experience From Mayoral ##########
##########################################################

# Women Candidates
m3a  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(m3a) # Positive, Null

# Women Candidates (ratio)
m3b  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp_ratio,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(m3b) # Positive, Null

# Women Candidates (pct)
m3c  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp_pct,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(m3c) # Positive, Null

# Figures
pdf(here('plots_tables', 'WomenCandidates_NoExp_Mayoral.pdf'), height = 8, width = 8)
genPlot(mydata = womenRDD, DV = 'totalWomen_noexp_frommp', dvName = '# of Women Candidates', run_var = 'run_var_woman', ylim = c(1, 5), t1 = 4.9, t2 = 4.4)
genPlot(mydata = womenRDD, DV = 'totalWomen_noexp_frommp_ratio', dvName = 'Ratio Women Candidates Per Seat', run_var = 'run_var_woman', ylim = c(0.1, 0.4), t1 = 0.4, t2 = 0.37)
genPlot(mydata = womenRDD, DV = 'totalWomen_noexp_frommp_pct', dvName = '% of Women Candidates', run_var = 'run_var_woman', ylim = c(35, 65), t1 = 65, t2 = 62)
dev.off()


